<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MtypeController extends Controller
{
    //查看
    public function getIndex(Request $request)
    {
        //dd($post);
        $count = 1;
        //判断是否搜索
        if($request -> input('keywords')){
            $post = DB::table('mtype')
                ->where('name','like','%'.$request->input('keywords').'%')
                ->paginate(10);
        }else{
            //获取数据库的数据
            $post = DB::table('mtype')->paginate(10);
        }
        //获取所有的数据
        $data = $request -> all();
        return view('Admin.mtype.index',['post'=>$post,'count' => $count,'data' => $data]);
	}



	//添加
	public function getAdd()
	{
		//dd($request->all());
		return view('Admin.mtype.add');
	}
	public function postInsert(Request $request)
	{
		//dd($request->all());
		$name=$request->only(['name']);
		//dd($name);
		$ress = DB::table('mtype')->where('name',$request->input('name'))->first();
    	//dd($user); 
    	if(empty($ress)){
    		
         //执行数据的插入
        $res = DB::table('mtype')->insert($name);
    	return redirect('/admin/mtype/index')->with('success','添加成功成功');
    	}else{
    		return back()->with('error','此板块已存在');
    	}

	}
	//删除
	public function getDelete($id)
	{
		//dd($id);
		$res=DB::table('mtype')->where('id',$id)->delete();
		if($res){
        return redirect('/admin/mtype/index')->with('success','删除成功');
    	}else{
    		return back()->with('error','删除失败');
    	}
    	
	}
	//修改
	//后台帖子模块修改
    public function getEdit($id)
    {
    	//获取值
    	$update = DB::table('mtype')->where('id',$id)->first();
    	//
    	return view('Admin.mtype.edit',['update'=>$update]);
    	//dd($update);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
    	$data = $request->only(['name']);
    	//dd($data);
    	$id = $request->input('id');
  
         //执行数据的插入
        $res = DB::table('mtype')->where('id',$id)->update($data);
        if($res){
        return redirect('/admin/mtype/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}

    }


}

